package com.ticket.sass.admin.business.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ticket.sass.admin.business.MemberCardBusiness;
import com.ticket.sass.admin.dto.input.member.MemberCardListInput;
import com.ticket.sass.admin.dto.input.member.MemberCardOrderRecordListInput;
import com.ticket.sass.admin.dto.output.member.MemberCardListOutput;
import com.ticket.sass.admin.dto.output.member.MemberCardOrderRecordListOutput;
import com.ticket.sass.common.entity.tenant.MemberCardOrderRecordEntity;
import com.ticket.sass.common.entity.tenant.MemberCardEntity;
import com.ticket.sass.admin.model.PageModel;
import com.ticket.sass.admin.service.impl.MemberCardOrderRecordServiceImpl;
import com.ticket.sass.admin.service.impl.MemberCardServiceImpl;
import com.ticket.sass.admin.util.ModelUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.TypeToken;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ywb
 * @date 2024-03-18 17:18
 */
@Slf4j
@Service
@AllArgsConstructor
public class MemberCardBusinessImpl implements MemberCardBusiness {
    
    final ModelUtil modelUtil;
    final MemberCardServiceImpl service;
    final MemberCardOrderRecordServiceImpl orderRecordService;
    
    @Override
    public PageModel<MemberCardListOutput> list(MemberCardListInput input) {
        IPage<MemberCardEntity> page = service.iPage(input);
        List<MemberCardListOutput> outputs = modelUtil.listToList(
            page.getRecords(),
            new TypeToken<List<MemberCardListOutput>>(){}.getType()
        );
        return new PageModel<>(page.getTotal(), outputs);
    }
    
    @Override
    public PageModel<MemberCardOrderRecordListOutput> orderRecordList(MemberCardOrderRecordListInput input) {
        IPage<MemberCardOrderRecordEntity> page = orderRecordService.getByCardIdWithPage(input.getId(), input.getPageNum(), input.getPageSize());
        List<MemberCardOrderRecordListOutput> outputs = page.getRecords().stream().map(MemberCardOrderRecordListOutput::new).collect(Collectors.toList());
        return new PageModel<>(page.getTotal(), outputs);
    }
    
}
